import fs from 'fs';
import querystring from 'querystring';
function login(req,res){
      let buf=Buffer.alloc(0);
        req.on('data',chunk=>{
            buf=Buffer.concat([buf,chunk])
        })
        req.on('end',()=>{
            let {user,pass}=querystring.parse(buf.toString())
            var userlist = JSON.parse( fs.readFileSync('./data/user.json','utf-8') )
            
            var index= userlist.find(item=>{
                return item.user===user
            })
            if(index){
                if(index.user===user&&index.pass===pass){
                    res.writeHead(200,'ok',{'Content-Type':'application/json;charset=utf-8'})
                    res.end('欢迎用户'+user+"登录")
                }else{
                    res.writeHead(200,'ok',{'Content-Type':'application/json;charset=utf-8'})
                    res.end('密码错误。')
                }
            }else{
                res.writeHead(200,'ok',{'Content-Type':'application/json;charset=utf-8'})
                res.end('你还没有注册')
                return
            }
        })
}
export default login;